package com.ctshk.rpc.order.scenic.service.impl.app;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctshk.common.constant.PayNotifyUrlConstants;
import com.ctshk.common.dto.Result;
import com.ctshk.common.dto.payment.CallPayDTO;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.enums.payment.PayCurrency;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.common.utils.thirdparty.TicketInternalApiUtil;
import com.ctshk.common.utils.thirdparty.airshowResp.AirshowOrderResp;
import com.ctshk.rpc.order.scenic.dto.AirshowOrderDTO;
import com.ctshk.rpc.order.scenic.entity.AirshowOrder;
import com.ctshk.rpc.order.scenic.entity.app.ScenicOrderReceipt;
import com.ctshk.rpc.order.scenic.mapper.app.ScenicOrderReceiptAppMapper;
import com.ctshk.rpc.order.scenic.req.AirshowOrderCancelReq;
import com.ctshk.rpc.order.scenic.req.AirshowOrderCreateReq;
import com.ctshk.rpc.order.scenic.req.AirshowOrderListReq;
import com.ctshk.rpc.order.scenic.req.app.ScenicOrderReceiptReq;
import com.ctshk.rpc.order.scenic.service.app.IAirshowAppOrderService;
import com.ctshk.rpc.order.scenic.service.app.IScenicOrderReceiptAppService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@DubboService
public class AirshowAppOrderServiceImpl implements IAirshowAppOrderService {

    @Override
    public Result createTicketOrder(AirshowOrderCreateReq airshowOrderCreateReq) {

        String s1 = JSONObject.toJSONString(airshowOrderCreateReq);

        String s = TicketInternalApiUtil.sendPost(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/create-ticket-product-order-not-pay", s1);

        return JSONObject.parseObject(s,Result.class);
    }

    @Override
    public Result createPackageOrder(AirshowOrderCreateReq airshowOrderCreateReq) {
        String s1 = JSONObject.toJSONString(airshowOrderCreateReq);

        String s = TicketInternalApiUtil.sendPost(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/create-package-product-order-not-pay", s1);

        return JSONObject.parseObject(s,Result.class);
    }

    @Override
    public Result payOrder(Long id) {
        String s = TicketInternalApiUtil.sendGet(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/pay-order", "id="+id);

        return JSONObject.parseObject(s,new TypeReference<Result<AirshowOrderDTO>>(){});
    }

    @Override
    public Result<AirshowOrderDTO> getOrderById(Long id) {


        String s = TicketInternalApiUtil.sendGet(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/get-order", "id="+id);

        return JSONObject.parseObject(s,new TypeReference<Result<AirshowOrderDTO>>(){});
    }

    @Override
    public Result getOrderListByMobile(AirshowOrderListReq airshowOrderListReq) {

        String s1 = JSONObject.toJSONString(airshowOrderListReq);

        String s = TicketInternalApiUtil.sendPost(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/get-orders", s1);

        return JSONObject.parseObject(s,Result.class);
    }

    @Override
    public Result cancelOrder(AirshowOrderCancelReq airshowOrderCancelReq) {

        String s1 = JSONObject.toJSONString(airshowOrderCancelReq);

        String s = TicketInternalApiUtil.sendPost(TicketInternalApiUtil.URL + "/crm/order/scenic/airshow-api/cancel-order", s1);

        return JSONObject.parseObject(s,Result.class);
    }

    @Override
    public CallPayDTO getCallPayInfo(Long id) {

        Result<AirshowOrderDTO> orderById = getOrderById(id);

        if(orderById.isSuccess()){
            AirshowOrderDTO data = orderById.getData();
            CallPayDTO callPayDTO = new CallPayDTO();
            callPayDTO.setSubject(data.getProductName()+" x"+data.getTotalNum());
            callPayDTO.setBody("2021第十三屆珠海航展電子票");
            callPayDTO.setCurrency(PayCurrency.HKD);
//            callPayDTO.setSalesmanId(1344284712813568L);
            callPayDTO.setOrderValidTime(data.getOrderTime().plusMinutes(30));
            callPayDTO.setTotalFee(data.getTotalAmount());

            return callPayDTO;

        }else{
            throw new BusinessException(SystemError.SCENIC_5016);
        }

    }

    @Override
    public Result payment(NotifyConsumeReq notifyConsumeReq) {

        Result<AirshowOrderDTO> orderById = getOrderById(Long.valueOf(notifyConsumeReq.getOrderNumber()));

        if(orderById.isSuccess()){
            return payOrder(Long.valueOf(notifyConsumeReq.getOrderNumber()));
        }

        throw new BusinessException(SystemError.SCENIC_5016);
    }
}
